/*
 * @Description: 表单
 * @Author: Zzt
 * @Date: 2022-08-09 14:59:01
 * @LastEditTime: 2022-08-11 10:54:26
 */

import { Form, FormInstance, Radio } from 'antd';
import TextArea from 'antd/lib/input/TextArea';
import { FC, useEffect } from 'react';
import { useDestructionContext } from '../../../contexts/DestructionContext';

interface TransferFormProps {
  data?: any;
  form: FormInstance;
  visible: boolean;
  closeDialog: any;
  disabled: any;
}

const TransferForm: FC<TransferFormProps> = (props) => {
  const { examineForm } = useDestructionContext();
  console.log('common form', props.data);
  const { data, form, visible, closeDialog, disabled } = props;
  console.log(disabled);

  useEffect(() => {
    if (visible) {
      if (data?.id) {
        form.setFieldsValue(data);
      }
    }
  }, [visible]);
  /* 
获取默认值
*/
  let states = 1;
  let opinions = '';
  if (data.state === 0) {
    states = 1;
    opinions = '';
  } else {
    states = data.state;
    opinions = data.opinions;
  }

  const onFinish = (values: any) => {
    console.log('finish', data, values);
    if (disabled) {
      closeDialog();
    } else {
      console.log(data);
      examineForm({ ...data, ...values });
      closeDialog();
    }
  };
  return (
    <Form
      initialValues={{ state: states, opinion: opinions }}
      form={form}
      labelCol={{ span: 5 }}
      wrapperCol={{ span: 16 }}
      onFinish={onFinish}
      disabled={disabled}
    >
      <Form.Item label="申请标题">{data.name}</Form.Item>
      <Form.Item label="审批结果" name="state">
        <Radio.Group>
          <Radio value={1}>审批通过</Radio>
          <Radio value={2}>审批退回</Radio>
        </Radio.Group>
      </Form.Item>
      <Form.Item label="意见" name="opinion">
        <TextArea rows={4} disabled={disabled} placeholder="请填写审批意见"></TextArea>
      </Form.Item>
    </Form>
  );
};
export default TransferForm;
